xen/arm: Fix race in RB-tree based P2M accounting
authorOleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Fri, 1 Jul 2022 07:57:42 +0000 (09:57 +0200)
committerBen Hutchings <benh@debian.org>
Sat, 23 Jul 2022 22:32:10 +0000 (23:32 +0100)
commite045e4f55f35ee102624c6051ed72711760c19ab
treedd9db11474f323caa1a21b54692eb939b8c9b345
parentaa86d80b6aaaaa28ade0cd00ec4cbf2d8a1c3f79
xen/arm: Fix race in RB-tree based P2M accounting

Origin: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit?id=43c8d33ce353091f15312cb6de3531517d7bba90
Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2022-33744

commit b75cd218274e01d026dc5240e86fdeb44bbed0c8 upstream.

During the PV driver life cycle the mappings are added to
the RB-tree by set_foreign_p2m_mapping(), which is called from
gnttab_map_refs() and are removed by clear_foreign_p2m_mapping()
which is called from gnttab_unmap_refs(). As both functions end
up calling __set_phys_to_machine_multi() which updates the RB-tree,
this function can be called concurrently.

There is already a "p2m_lock" to protect against concurrent accesses,
but the problem is that the first read of "phys_to_mach.rb_node"
in __set_phys_to_machine_multi() is not covered by it, so this might
lead to the incorrect mappings update (removing in our case) in RB-tree.

In my environment the related issue happens rarely and only when
PV net backend is running, the xen_add_phys_to_mach_entry() claims
that it cannot add new pfn <-> mfn mapping to the tree since it is
already exists which results in a failure when mapping foreign pages.

But there might be other bad consequences related to the non-protected
root reads such use-after-free, etc.

While at it, also fix the similar usage in __pfn_to_mfn(), so
initialize "struct rb_node *n" with the "p2m_lock" held in both
functions to avoid possible bad consequences.

This is CVE-2022-33744 / XSA-406.

Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>
Signed-off-by: Juergen Gross <jgross@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Gbp-Pq: Topic bugfix/all
Gbp-Pq: Name xen-arm-fix-race-in-rb-tree-based-p2m-accounting.patch
arch/arm/xen/p2m.c